import request from './request'

// ==================== 科室管理 ====================

// 获取科室树形列表
export function getDepartmentTree(params) {
  return request({
    url: '/business/departments',
    method: 'get',
    params: { ...params, tree: true }
  })
}

// 获取科室医生列表
export function getDepartmentDoctors(departmentId) {
  return request({
    url: `/business/department/${departmentId}/doctors`,
    method: 'get'
  })
}

// ==================== 挂号管理 ====================
// 获取挂号列表
export function getRegistrationList(params) {
  return request({
    url: '/business/registrations',
    method: 'get',
    params
  })
}

// 更新挂号状态
export function updateRegistrationStatus(registrationId, status, remark) {
  return request({
    url: `/business/registration/${registrationId}/status`,
    method: 'put',
    data: { status, remark }
  })
}

// 获取挂号详情
export function getRegistrationDetail(registrationId) {
  return request({
    url: `/business/registration/${registrationId}`,
    method: 'get'
  })
}

// ==================== 就诊记录管理 ====================
// 获取就诊记录列表
export function getMedicalRecordList(params) {
  return request({
    url: '/business/medical-records',
    method: 'get',
    params
  })
}

// 获取就诊记录详情
export function getMedicalRecordDetail(recordId) {
  return request({
    url: `/business/medical-record/${recordId}`,
    method: 'get'
  })
}

// 添加就诊记录
export function addMedicalRecord(data) {
  return request({
    url: '/business/medical-record',
    method: 'post',
    data
  })
}

// 更新就诊记录
export function updateMedicalRecord(data) {
  return request({
    url: '/business/medical-record',
    method: 'put',
    data
  })
}

// 删除就诊记录
export function deleteMedicalRecord(recordId) {
  return request({
    url: `/business/medical-record/${recordId}`,
    method: 'delete'
  })
}

// ==================== 药方管理 ====================
// 获取药方列表
export function getPrescriptionList(params) {
  return request({
    url: '/business/prescriptions',
    method: 'get',
    params
  })
}

// 添加药方
export function addPrescription(data) {
  return request({
    url: '/business/prescription',
    method: 'post',
    data
  })
}

// ==================== 药品管理 ====================
// 获取药品列表
export function getMedicineList(params) {
  return request({
    url: '/business/medicines',
    method: 'get',
    params
  })
}

// 获取药品详情
export function getMedicineDetail(medicineId) {
  return request({
    url: `/business/medicine/${medicineId}`,
    method: 'get'
  })
}

// 获取药品库存预警
export function getMedicineWarnings(params) {
  return request({
    url: '/business/medicines/warning',
    method: 'get',
    params
  })
}

// 获取库存不足药品
export function getLowStockMedicines() {
  return request({
    url: '/business/medicines/low-stock',
    method: 'get'
  })
}

// 获取即将过期药品
export function getNearExpiryMedicines() {
  return request({
    url: '/business/medicines/near-expiry',
    method: 'get'
  })
}

// 获取已过期药品
export function getExpiredMedicines() {
  return request({
    url: '/business/medicines/expired',
    method: 'get'
  })
}

// 获取药品库存统计
export function getMedicineStatistics() {
  return request({
    url: '/business/medicines/statistics',
    method: 'get'
  })
}

// 药品入库
export function addMedicineStock(medicineId, quantity, remark) {
  return request({
    url: `/business/medicine/${medicineId}/add-stock`,
    method: 'post',
    params: { quantity, remark }
  })
}

// 药品出库
export function reduceMedicineStock(medicineId, quantity, remark) {
  return request({
    url: `/business/medicine/${medicineId}/reduce-stock`,
    method: 'post',
    params: { quantity, remark }
  })
}

// ==================== 排班管理 ====================
/**
 * 获取排班列表
 * @param {Object} params - 查询参数
 * @param {string} [params.startDate] - 开始日期
 * @param {string} [params.endDate] - 结束日期
 * @param {number} [params.departmentId] - 科室ID
 * @param {number} [params.status] - 状态
 * @param {number} [params.page] - 页码
 * @param {number} [params.size] - 每页大小
 * @returns {Promise} 返回排班列表数据
 */
export function getScheduleList(params) {
  return request({
    url: '/business/schedules',
    method: 'get',
    params
  })
}

/**
 * 获取排班详情
 * @param {number} scheduleId - 排班ID
 * @returns {Promise} 返回排班详细信息
 */
export function getScheduleDetail(scheduleId) {
  return request({
    url: `/business/schedule/${scheduleId}`,
    method: 'get'
  })
}

/**
 * 创建排班
 * @param {Object} data - 排班信息
 * @param {number} data.doctorId - 医生ID
 * @param {number} data.departmentId - 科室ID
 * @param {string} data.scheduleDate - 排班日期
 * @param {number} data.timeSlot - 时段：1-上午，2-下午，3-晚上
 * @param {string} data.startTime - 开始时间
 * @param {string} data.endTime - 结束时间
 * @param {number} data.registrationFee - 挂号费用
 * @param {number} data.availableCount - 可预约数量
 * @param {number} [data.status] - 状态：0-停诊，1-正常，2-满号
 * @param {string} [data.remark] - 备注
 * @returns {Promise} 返回创建结果
 */
export function createSchedule(data) {
  return request({
    url: '/business/schedule',
    method: 'post',
    data
  })
}

/**
 * 更新排班
 * @param {Object} data - 排班信息
 * @param {number} data.id - 排班ID
 * @param {number} [data.registrationFee] - 挂号费用
 * @param {number} [data.availableCount] - 可预约数量
 * @param {number} [data.status] - 状态
 * @param {string} [data.remark] - 备注
 * @returns {Promise} 返回更新结果
 */
export function updateSchedule(data) {
  return request({
    url: '/business/schedule',
    method: 'put',
    data
  })
}

// 更新排班状态
export function updateScheduleStatus(scheduleId, status, remark) {
  return request({
    url: `/business/schedule/${scheduleId}/status`,
    method: 'put',
    params: { status, remark }
  })
}

// 删除排班
export function deleteSchedule(scheduleId) {
  return request({
    url: `/business/schedule/${scheduleId}`,
    method: 'delete'
  })
}

// 获取排班统计
export function getScheduleStatistics(params) {
  return request({
    url: '/business/schedule/statistics',
    method: 'get',
    params
  })
}

// ==================== 病房管理 ====================
/**
 * 获取病房列表
 * @param {Object} params - 查询参数
 * @param {string} [params.keyword] - 搜索关键词
 * @param {number} [params.departmentId] - 科室ID
 * @param {number} [params.type] - 病房类型
 * @param {number} [params.status] - 状态
 * @param {number} [params.floor] - 楼层
 * @returns {Promise} 返回病房列表数据
 */
export function getWardList(params) {
  return request({
    url: '/business/wards',
    method: 'get',
    params
  })
}

/**
 * 获取病房详情
 * @param {number} wardId - 病房ID
 * @returns {Promise} 返回病房详细信息
 */
export function getWardDetail(wardId) {
  return request({
    url: `/business/ward/${wardId}`,
    method: 'get'
  })
}

/**
 * 更新病房信息
 * @param {Object} wardData - 病房信息
 * @returns {Promise} 返回更新结果
 */
export function updateWard(wardData) {
  return request({
    url: '/admin/ward',
    method: 'put',
    data: wardData
  })
}

/**
 * 添加病房
 * @param {Object} wardData - 病房信息
 * @returns {Promise} 返回添加结果
 */
export function addWard(wardData) {
  return request({
    url: '/admin/ward',
    method: 'post',
    data: wardData
  })
}

// 获取护士列表
export function getNurseList(params) {
  return request({
    url: '/admin/users',
    method: 'get',
    params
  })
}

// 删除病房
export function deleteWard(wardId) {
  return request({
    url: `/admin/ward/${wardId}`,
    method: 'delete'
  })
}

// 更新床位占用情况
export function updateBedOccupancy(wardId, occupiedBeds) {
  return request({
    url: `/admin/ward/${wardId}/beds`,
    method: 'put',
    data: { occupiedBeds }
  })
}

// 根据科室获取病房列表
export function getWardsByDepartment(departmentId) {
  return request({
    url: `/business/department/${departmentId}/wards`,
    method: 'get'
  })
}

// 获取可用床位
export function getAvailableBeds(wardId) {
  return request({
    url: `/business/ward/${wardId}/available-beds`,
    method: 'get'
  })
}

// 分配床位
export function assignBed(data) {
  return request({
    url: `/business/medical-record/${data.recordId}/assign-bed`,
    method: 'post',
    data
  })
}

// 办理入院
export function admission(recordId) {
  return request({
    url: `/business/medical-record/${recordId}/admission`,
    method: 'post'
  })
}

// 办理出院
export function discharge(recordId) {
  return request({
    url: `/business/medical-record/${recordId}/discharge`,
    method: 'post'
  })
} 